package omg.loot;
import java.util.HashMap;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;

import com.jogamp.opengl.util.texture.Texture;


public class Renderer extends GLCanvas implements GLEventListener {
   private static final long serialVersionUID = 1L;
   GameRunnable game;

   //Textures:
   TextureManager textureManager;
   Texture atlas1;
   //Sprites:
   Sprite tile_land, tile_water, tile_start, tile_end, tile_swap;
   Sprite vohveli, runner;
   Square select = new Square(Tile.size, Tile.size);
   SpriteBatch batch = new SpriteBatch(10000);

   Camera cam;

   public Renderer(final GLCapabilities caps, final GameRunnable gt){
      super(caps);
      game = gt;
      this.cam = game.cam;
   }

   @Override
   public void init(final GLAutoDrawable drawable) {
      final GL2 gl = drawable.getGL().getGL2();
      loadTextures(gl);
      gl.glMatrixMode(GL2.GL_PROJECTION);
      gl.glLoadIdentity();
      gl.glOrtho(0, Main.WIDTH, Main.HEIGHT, 0, 0, 1);
      gl.glDisable(GL2.GL_DEPTH_TEST);
      gl.glMatrixMode(GL2.GL_MODELVIEW);
      gl.glLoadIdentity();

      gl.glDisable(GL2.GL_DEPTH_TEST);
      gl.glEnable(GL2.GL_BLEND);
      gl.glEnable(GL2.GL_LINE_SMOOTH);
      gl.glEnable(GL2.GL_POINT_SMOOTH);

      gl.glEnable(GL2.GL_TEXTURE_2D);       //Enable Texture Mapping
      gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
      gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
      gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_FASTEST);
      gl.glBlendFunc(GL2.GL_ONE, GL2.GL_ONE_MINUS_SRC_ALPHA); //Enable transparency
      gl.glBindTexture(GL2.GL_TEXTURE_2D, atlas1.getTextureObject());
   }

   public void loadTextures(final GL2 gl) {
      textureManager = new TextureManager(gl);
      atlas1 = textureManager.loadTextureAtlas("/omg/loot/gfx/textures.png");
      final HashMap<String, Sprite> textures = textureManager.parseXML("/omg/loot/gfx/textures.xml");
      runner = textures.remove("waffle");
      runner.prepareForBatch(10000);
      tile_water = textures.remove("water");
      tile_land = textures.remove("grass");
   }

   @Override
   public void display(final GLAutoDrawable drawable) {
      final GL2 gl = drawable.getGL().getGL2();
      gl.glClear(GL.GL_COLOR_BUFFER_BIT);
      gl.glMatrixMode(GL2.GL_PROJECTION);
      gl.glLoadIdentity();
      cam.saveValues(); //Talletetaan nykyiset arvot väliaikaismuuttujiin jottei arvot muutu kesken piirron
      final float xz = (Main.WIDTH-Main.WIDTH/cam.t_zoom)*0.5f;
      final float yz = (Main.HEIGHT-Main.HEIGHT/cam.t_zoom)*0.5f;
      gl.glOrtho(xz, Main.WIDTH-xz, Main.HEIGHT-yz, yz, 0, 1); //Säädetään zoomi
      gl.glMatrixMode(GL2.GL_MODELVIEW);
      gl.glLoadIdentity();
      gl.glTranslatef(cam.t_x, cam.t_y, 0); //Liikutetaan kameraa
      gl.glPushMatrix();
      gl.glColor3f(1, 1, 1);
      batch.drawBatch(gl, this, game.map.tileMap); //Kenttä
      if (game.mouse.isOnPlayfield) {
         gl.glPushMatrix();
         gl.glTranslatef(game.mouse.tileX*Tile.size, game.mouse.tileY*Tile.size, 0); //Maali
         select.draw(gl);
         gl.glPopMatrix();
      }
      batch.drawBatch(gl, runner, game.v, 1); //Vihu
      gl.glColor3f(0, 1, 0);
      gl.glPopMatrix();
      gl.glPushMatrix();
      textureManager.setSprite(runner);
      gl.glTranslatef(game.start.x*Tile.size, game.start.y*Tile.size, 0); //Lähtö
      textureManager.draw(gl);
      gl.glPopMatrix();
      gl.glColor3f(1, 0, 0);
      gl.glTranslatef(game.end.x*Tile.size, game.end.y*Tile.size, 0); //Maali
      textureManager.draw(gl);
   }

   @Override
   public void dispose(final GLAutoDrawable drawable) {
      textureManager.kill();
   }

   @Override
   public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int w, final int h) {
   }
}